package es.pawer.android.listacompras.biz;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;
import es.pawer.android.listacompras.data.DataException;
import es.pawer.android.listacompras.data.ItemDao;
import es.pawer.android.listacompras.data.ItemTO;
import es.pawer.android.listacompras.data.ShoppingListDao;
import es.pawer.android.listacompras.data.ShoppingListTO;

public class ShoppingListFacade {

	private ItemDao itemDao;
	private ShoppingListDao sDao;

	/**
	 * The default constructor
	 */
	public ShoppingListFacade() {
		itemDao = new ItemDao();
		sDao= new ShoppingListDao();
	}

	/**
	 * Obtains the items of the given list.
	 * 
	 * @param l
	 *            The list whose items are asked.
	 * @return A List of names of the items associated to the list.
	 */
	public ShoppingList populateList(ShoppingList l) {
		List<ItemTO> items = itemDao.getByList(l.getId());
		List<Item> result = new ArrayList<Item>();
		if (items != null && !items.isEmpty()) {
			for (ItemTO i : items) {
				Item aux = new Item(i);
				result.add(aux);
			}

		}
		l.setItemList(result);
		return l;
	}

	/**
	 * Adds an item to the given list.
	 * 
	 * @param g
	 *            the item to be saved.
	 * @throws DataException
	 *             If the item has no name, no associated list or is null.
	 */
	public void addItem(ItemTO g) throws DataException {
		if (g == null) {
			throw new DataException("item object is null");
		}
		if (g.getName() == null || g.getName().length() == 0) {
			throw new DataException("item object has no name");
		}
		itemDao.insert(g);
	}

	/**
	 * Removes a given item from a ShoppingList.
	 * 
	 * @param g
	 *            the item to be removed.
	 * @throws DataException
	 *             if g is null or has no name.
	 */
	public void removeItem(ItemTO g) throws DataException {
		if (g == null) {
			throw new DataException("item object is null");
		}
		if (g.getName() == null || g.getName().length() == 0) {
			throw new DataException("item object has no name");
		}
		itemDao.delete(g);
	}

	public List<ShoppingList> getAllLists() {
		List<ShoppingList> result= new ArrayList<ShoppingList>();
		List<ShoppingListTO> lists = this.sDao.getAll();
		if (lists!=null && !lists.isEmpty()) {
			for (ShoppingListTO to: lists) {
				ShoppingList sl= new ShoppingList(to);
				result.add(sl);
			}
		}
		return result;
	}


	public void saveList(ShoppingList sl) throws DataException {
		if (sl!=null) {
			this.sDao.insert(sl);
			if (Log.isLoggable(null, 0))
				Log.i("DATA", "ShoppingList inserted");

			if (sl.getItemList()!=null && !sl.getItemList().isEmpty()) {
				for (Item i: sl.getItemList()) {
					i.setListId(sl.getId());
					long iid=this.itemDao.insert(i);
					i.setListId(iid);
					Log.i("DATA", "Item "+i+" inserted");
				}
			}
		}

	}

	public void updateList(ShoppingList sl) throws DataException {
		if (sl!=null) {
			if (Log.isLoggable(null, 0))
				Log.i("DATA", "ShoppingList inserted");

			if (sl.getItemList()!=null && !sl.getItemList().isEmpty()) {
				for (Item i: sl.getItemList()) {
					i.setListId(sl.getId());
					long iid=this.itemDao.update(i);
					i.setListId(iid);
					Log.i("DATA", "Item "+i+" inserted");
				}
			}
		}
	}
}
